<template>
  <div>
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="100px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item label="所属数据源">
        <el-input disabled v-model="dataForm.paramKey" />
      </el-form-item>
      <el-form-item label="数据库名称" prop="paramValue">
        <el-input disabled v-model="dataForm.paramValue" />
      </el-form-item>
      <el-form-item label="数据表名称" prop="paramValue">
        <el-input v-model="dataForm.paramValue" placeholder="数据表名称" />
      </el-form-item>
      <el-form-item label="列表管理">
        <el-table
          :data="dataForm.tableData"
          height="200"
          border
          style="width: 100%"
        >
          <el-table-column prop="name" label="列名">
            <template slot-scope="scope">
              <el-form-item
                :prop="'tableData.' + scope.$index + '.name'"
                :rules="dataRule.name"
              >
                <el-input
                  v-model="scope.row.name"
                  size="mini"
                  placeholder="输入值"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="type" label="类型">
            <template slot-scope="scope">
              <el-form-item
                :prop="'tableData.' + scope.$index + '.type'"
                :rules="dataRule.type"
              >
                <el-select
                  size="mini"
                  v-model="scope.row.type"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="i in typeList"
                    :key="i.value"
                    :label="i.type"
                    :value="i.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="长度">
            <template slot-scope="scope">
              <el-form-item
                :prop="'tableData.' + scope.$index + '.length'"
                :rules="dataRule.length"
              >
                <el-input-number
                  size="mini"
                  v-model="scope.row.length"
                  :min="1"
                ></el-input-number>
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column width="60" align="center">
            <template slot="header" slot-scope>
              <i
                title="添加查询条件"
                @click="addQueryTable"
                class="el-icon-circle-plus-outline"
              ></i>
            </template>
            <template slot-scope="scope">
              <i
                title="删除该查询条件"
                @click="delQueryTable(scope.$index)"
                class="el-icon-delete"
              ></i>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      typeList: [
        { name: "整型", value: "byte 、short 、int 、long" },
        { name: "浮点型", value: "float 、 double" },
        { name: "字符型", value: "char" },
        { name: "布尔型", value: "boolean" }
      ],
      dataForm: {
        id: 0,
        paramKey: "",
        paramValue: "",
        tableData: [],
        remark: ""
      },

      dataRule: {
        paramValue: [{ required: true, message: "不能为空", trigger: "blur" }],
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        length: [{ required: true, message: "不能为空", trigger: "blur" }],
        type: [{ required: true, message: "不能为空", trigger: "change" }]
      }
    };
  },
  methods: {
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$emit("close");
        }
      });
    },
    addQueryTable() {
      this.dataForm.tableData.push({
        name: "",
        type: "",
        length: "and"
      });
    },
    delQueryTable(index) {
      this.dataForm.tableData.splice(index, 1);
    }
  }
};
</script>

<style scoped lang="scss">
.el-form {
  .el-table::v-deep {
    // background: transparent;
    .el-form-item__content {
      line-height: 0.25rem;
      .el-input {
        height: 0.3rem;
      }
    }

    td,
    th.is-leaf {
      line-height: 0.2rem;
      padding: 0.05rem;
      border-bottom: 1px solid #32333a;
      border-right: 1px solid #32333a;
    }
    th,
    tr {
      background-color: transparent;
    }
  }
}
</style>
